package com.nstar.base.controls
{
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import mx.controls.TextInput;
	import mx.controls.Button;
	import mx.events.FlexEvent;
	import com.nstar.base.util.ColorUtils;

	/**
	 * @class ClearTextInput
	 * @brief A TextInput with button to clear the text.
	 * @author Jove
	 * @version 1.0
	 * @modified by Li Jialing
	 */
	public class ClearTextInput extends TextInput{
		
		[Embed(source="../assets/16X16/WindowCloseButton.gif")]
		private var clearButtonImg:Class;
		[Embed(source="../assets/16X16/WindowCloseButton2.gif")]
		private var clearButtonImg2:Class;
		
		private var clearButton:Button;
		
		public var defultText:String = ""
		public var defultTextColor:String = "#AAAAAA"
		
		public function ClearTextInput(){
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, addButton);
			this.addEventListener("textChanged", showClearButton);
			this.addEventListener(FocusEvent.FOCUS_IN, this_focusInHandler)
			this.addEventListener(FocusEvent.FOCUS_OUT, this_focusOutHandler)
		}
		
		public function set value(v:String):void {
			this.text = v;
		}
		
		public function get value():String {
			return this.text;
		}
		
		private function addButton(event:FlexEvent):void{
			if(clearButton == null){
				clearButton = new Button();
				clearButton.width=10;
				clearButton.height=10;
				clearButton.y = (this.height - 10) / 2;
				clearButton.x = this.width - 10 - (this.height - 10) / 2;
				clearButton.focusEnabled=false;
				clearButton.setStyle("upSkin", clearButtonImg);
				clearButton.setStyle("overSkin", clearButtonImg2);
				clearButton.setStyle("downSkin", clearButtonImg2);
				clearButton.addEventListener(MouseEvent.CLICK, clearButton_clickHandler);
				clearButton.visible = false;
				this.addChild(clearButton);
				if(text == ""){
					setDefaultText();
				}
			}
		}
		
		private function clearButton_clickHandler(event:MouseEvent):void{
			this.text = "";
			clearButton.visible = false;
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		
		private function this_focusInHandler(event:FocusEvent):void{
			if(text == ""){
				this.text = "";
				this.textField.text = "";
				this.textField.textColor = this.getStyle("color");
				clearButton.visible = false;
			}
		}
		
		private function this_focusOutHandler(event:FocusEvent):void{
			if(text == ""){
				setDefaultText();
			}
		}
		
		private function setDefaultText():void{
			this.text = "";
			this.textField.text = defultText;
			this.textField.textColor = ColorUtils.convertColor(defultTextColor);
			clearButton.visible = false;
		}
				
		private function showClearButton(event:Event):void{
			if(text != ""){
				clearButton.visible = true;
			}else{
				clearButton.visible = false;
			}
			this.textField.textColor = this.getStyle("color");
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			if(clearButton != null){
				clearButton.x = this.width - 10 - (this.height - 10) / 2;
				clearButton.y = (this.height - 10) / 2;
			}
		}
	}
}